<template>
  <div class="user-wrapper">
    <div class="content-box">
      <a-icon type="notification"/>
      <span>：欢迎<span style="color: #1890ff;">{{ nickname }}</span>使用本系统，祝您工作愉快！</span>
      <notice-icon class="action"/>
      <a-dropdown>
        <span class="action ant-dropdown-link user-dropdown-menu">
          <!-- <a-avatar class="avatar" size="small" :src="avatar"/> -->
          <a-icon type="setting" />
          
        </span>
        <a-menu slot="overlay" class="user-dropdown-menu-wrapper">
          <a-menu-item key="0">
            <a href="javascript:;" @click="update">
              <a-icon type="user"/>
              <span>修改密码</span>
            </a>
          </a-menu-item>
          <a-menu-divider/>
          <a-menu-item key="1">
            <a href="javascript:;" @click="handleLogout">
              <a-icon type="logout"/>
              <span>退出登录</span>
            </a>
          </a-menu-item>
        </a-menu>
      </a-dropdown>
      <a-modal
        :title="title"
        width="40%"
        :visible="visible"
        :confirmLoading="confirmLoading"
        @ok="submit"
        @cancel="cancel">
        <a-spin :spinning="confirmLoading">
          <a-form :form="form">
            <a-form-item label="原始密码" :validate-status="validateStatus" :help="help" :labelCol="labelCol" :wrapperCol="wrapperCol" >
              <a-input type="password" v-decorator="['oldPassword', {rules: [{required: true, message: '请输原始密码！'}]}]" />
            </a-form-item>
            <a-form-item label="新密码" :labelCol="labelCol" :wrapperCol="wrapperCol" >
              <a-input type="password" @blur="handleConfirmBlur" v-decorator="['newPassword', {rules: [{required: true, message: '请输入新密码！'},{validator: validateToNextPassword}]}]" />
            </a-form-item>
            <a-form-item label="确认新密码" :labelCol="labelCol" :wrapperCol="wrapperCol" >
              <a-input type="password" v-decorator="['confirmPassword', {rules: [{required: true, message: '请输入新密码！'},{validator: compareToFirstPassword}]}]" />
            </a-form-item>
          </a-form>
        </a-spin>
      </a-modal>
    </div>
  </div>
</template>

<script>
import Vue from 'vue'
import NoticeIcon from '@/components/NoticeIcon'
import { mapActions, mapGetters } from 'vuex'
import { ACCESS_TOKEN } from '@/store/mutation-types'
import router from '@/router'
export default {
  name: 'UserMenu',
  components: {
    NoticeIcon
  },
  computed: {
    ...mapGetters(['nickname', 'avatar'])

  },
  data () {
    return {
      validateStatus:'success',
      help:'',
      title:'',
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this),
      labelCol: { xs: { span: 24 }, sm: { span: 5 } },
      wrapperCol: { xs: { span: 24 }, sm: { span: 15 } },
    }
  },
  methods: {
    ...mapActions(['Logout']),
    handleConfirmBlur(e) {
      const value = e.target.value
      this.confirmDirty = this.confirmDirty || !!value
    },
    compareToFirstPassword(rule, value, callback) {
      const form = this.form
      if (value && value !== form.getFieldValue('newPassword')) {
        callback('两次密码不一致!')
      } else {
        callback()
      }
    },
    validateToNextPassword(rule, value, callback) {
      const form = this.form
      if (value && this.confirmDirty) {
        form.validateFields(['confirmPassword'], { force: true })
      }
      callback()
    },
    update() {
      this.visible = true
      this.form = this.$form.createForm(this)
      this.validateStatus = 'success'
      this.help = ''
      this.title = '修改密码'
    },
    cancel () {
      this.visible = false
      this.formData = {}
    },
    submit () {
      const { form: { validateFields } } = this
      const _this = this
      this.confirmLoading = true
      validateFields((errors, values) => {
        if (!errors) {
          _this.myRequest('/changePassword',values)
            .then(res => {
              if(res.success){
                // Vue.ls.remove(ACCESS_TOKEN)
                // const result = res.result
                // console.log('----result.Authorization--------------------------',result.Authorization)
                // Vue.ls.set(ACCESS_TOKEN, result.Authorization, 7 * 24 * 60 * 60 * 1000)
                // _this.visible = false
                // _this.confirmLoading = false
                
                
                  Vue.ls.remove(ACCESS_TOKEN)
                 
                  //store.getters.roles = null
                   console.log(router)
                  return this.Logout({}).then(() => {
                     window.location.reload()
                  }).catch(err => {
                    this.$message.error({
                      title: '错误',
                      description: err.message
                    })
                  })
                
                
              }else{
                _this.validateStatus = 'error'
                _this.help = '原始密码不正确'
                 _this.confirmLoading = false
              }
            })
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleLogout () {
      this.$confirm({
        title: '提示',
        content: '真的要注销登录吗 ?',
        onOk: () => {
          Vue.ls.remove(ACCESS_TOKEN)
         
          //store.getters.roles = null
           console.log(router)
          return this.Logout({}).then(() => {
             window.location.reload()
          }).catch(err => {
            this.$message.error({
              title: '错误',
              description: err.message
            })
          })
        },
        onCancel () {
        }
      })
    }
  }
}
</script>
